var AldeasInfantiles = angular.module("AldeasInfantiles", []);

AldeasInfantiles.controller("Dorps", function($scope, $http, $timeout) {

  $scope.countries = []; //Array with all registred countries in the database
  $scope.dorps = []; //Array with all registred users in the database
  $scope.hiddenClass = "hidden"; //Used to show or hide the error message in the UI
  $scope.alertClass = ""; //Used to display a message in the UI
  $scope.errorMessage = ""; //Used to change the class of the message div in the UI
  $scope.dorp = {}; //Donor object
  $scope.selectedCountry = ""; //Selected donor country value

  //Requests all countries
  $scope.requestCountries = function () {
    $http({
          async: false,
          method: 'POST', 
          url: currentPath + 'countries/returncountries/'
        }).
        success(function(data) {
          $scope.countries = data;
          $scope.selectedCountry = data[0].id_pais;
        }).
        error(function(data, status) {
          console.log(e.message);
        });
  };

  //Displays all donors in the UI
  $scope.showDorps = function () {
    $http({
            method: 'POST', 
            url: currentPath + 'dorps/returndorpsbycountry/' + $scope.selectedCountry
        }).
        success(function(data) {
          $scope.dorps = data;
        }).
        error(function(data, status) {
           console.log(e.message);
        });
  };

  //Receives the id of an user to get the information from the DB
  $scope.getDorpById = function (dorp) {
    var id = dorp.id_programa;
    $http({
            method: 'POST', 
            url: currentPath + 'dorps/returndorpbyid/' + id
        }).
        success(function(data) {
          $scope.dorp.dorpid = data[0].id_programa;
          $scope.dorp.dorpname = data[0].ds_programa;
          $scope.dorp.dorpdir = data[0].ds_direccion;
          $scope.selectedCountry = data[0].id_pais;
          $('#modal-dorps').modal('hide');
        }).
        error(function(data, status) {
           console.log(e.message);
        });
  };

  $scope.getParams = function ($dorp) {
    $params = $.param({
      'dorpid': $dorp.dorpid,
      'dorpname': $dorp.dorpname,
      'dorpdir': $dorp.dorpdir,
      'dorpcountry': $scope.selectedCountry
    })
    $scope.saveDorp($params);
  };

  //Saves the user information in the database
  $scope.saveDorp = function ($params) {
    $http({
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            url: currentPath + 'dorps/savedorp/',
            method: 'POST',
            data: $params,
            
        }).
        success(function(data, status) {
          if(data.response == "success"){
            $scope.errorMessage = "Datos guardatos correctamente";
            $scope.hiddenClass = "";
            $scope.alertClass = "alert-success";
            $scope.resetForm();
          }else if(data.response == "incomplete_form"){
            $scope.hiddenClass = "";
            $scope.alertClass = "alert-danger";
            $scope.errorMessage = "Debes escribir todos los datos del formulario.";
          }else if(data.response == "failed"){
            $scope.hiddenClass = "";
            $scope.alertClass = "alert-danger";
            $scope.errorMessage = "Algo fallo al guardar los datos, intentalo de nuevo.";
          }
        }).
        error(function(data, status) {
          $scope.hiddenClass = "";
          $scope.errorMessage = data;
        });
  };

  $scope.resetForm = function() {
    $scope.dorp.dorpid = "";
    $scope.dorp.dorpname = "";
    $scope.dorp.dorptel = "";
    $scope.dorp.dorpmail = 1;
    $scope.dorp.dorpdir = "";
    $scope.selectedCountry = 1;
    $timeout(countUp, 5000);
  };

  var countUp = function() {
    $scope.hiddenClass = "hidden";
  }

  $scope.setDefaultSettings = function () {
    $scope.requestCountries(); 
    //$scope.requestTypeUsers();
    //$scope.adduserstate = 1;
  }

  $scope.setDefaultSettings();

});